package com.sdcqjy.property.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.mode.LawRegulationsDetailsMode;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.contract.LawRegulationsDetailsContract;
import com.sdcqjy.property.presenter.contract.a;
import com.sdcqjy.property.utils.GetBookMarkUtils;

import java.util.List;

/**
 * Created by 磁磁帅 on 2018/7/8.
 * 功能：
 */

public class LawRegulationsDetailsPresenter extends BaseContract.Presenter<LawRegulationsDetailsContract.View> {
    public LawRegulationsDetailsPresenter(LawRegulationsDetailsContract.View baseView) {
        super(baseView);
    }

    public void getBookMark() {
        if (baseView != null) baseView.openLoadDialog();
        getBookMark_();
    }

    public void getBookMark_() {
        List<LawRegulationsDetailsMode> modeList = GetBookMarkUtils.getInstance().getBookMark();
        if (modeList == null) {
            LoginMode mode = LoginMode.getMode(AppLL.getAppLL());
            new HttpClient().setHttpType(HttpClient.HttpType.JSON)
                    //.addParam("lawID", lawID)
                    .addParam("userID", mode.id)
                    .httpRequest(a.GetBookMark, new BaseRequestBack<BaseRet<List<LawRegulationsDetailsMode>>>() {
                        public BaseRet<List<LawRegulationsDetailsMode>> jsonToMode(String jsonStr) {
                            BaseRet<List<LawRegulationsDetailsMode>> ret = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<LawRegulationsDetailsMode>>>() {
                            }.getType());
                            return ret;
                        }

                        public void onResponseSucc(BaseRet<List<LawRegulationsDetailsMode>> ret) {
                            if (baseView != null) {
                                baseView.dismissDialog();
                                if (ret.isOk()) {
                                    List<LawRegulationsDetailsMode> data = ret.data;
                                    GetBookMarkUtils.getInstance().setBookMark(data);
                                    baseView.getBookMarkRet(data);
                                } else {
                                    baseView.applyError(ret.msg);
                                }
                            }
                        }

                        public void onResponseFail(String retStr) {
                            if (baseView != null) {
                                baseView.dismissDialog();
                                baseView.applyError(retStr);
                            }
                        }
                    });
        } else {
            if (baseView != null) {
                baseView.dismissDialog();
                baseView.getBookMarkRet(modeList);
            }
        }
    }

    public void addBookMark(LawRegulationsDetailsMode detailsMode) {
        LoginMode mode = LoginMode.getMode(AppLL.getAppLL());
        new HttpClient().setHttpType(HttpClient.HttpType.JSON)
                .addParam("lawID", detailsMode.id)
                .addParam("userID", mode.id)
                .httpRequest(a.AddBookMark, new BaseRequestBack<BaseRet>() {
                    public BaseRet jsonToMode(String jsonStr) {
                        return new Gson().fromJson(jsonStr, BaseRet.class);
                    }

                    public void onResponseSucc(BaseRet ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                detailsMode.lawID = detailsMode.id;
                                GetBookMarkUtils.getInstance().addBookMark(detailsMode);
                                baseView.addBookMarkSucc();
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void delBookMark(LawRegulationsDetailsMode detailsMode) {
        LoginMode mode = LoginMode.getMode(AppLL.getAppLL());
        new HttpClient().setHttpType(HttpClient.HttpType.JSON)
                .addParam("lawID", detailsMode.lawID)
                .addParam("userID", mode.id)
                .httpRequest(a.DelBookMark, new BaseRequestBack<BaseRet>() {
                    public BaseRet jsonToMode(String jsonStr) {
                        return new Gson().fromJson(jsonStr, BaseRet.class);
                    }

                    public void onResponseSucc(BaseRet ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                GetBookMarkUtils.getInstance().delBookMark(detailsMode);
                                baseView.delBookMarkSucc();
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }
}
